package com.hxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.aspect.satoken.StpMobileUtil;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.util.MarketThreadLocalUtil;
import com.hxzy.controller.mobile.cart.req.CartAddReq;
import com.hxzy.controller.mobile.cart.req.MobileAddCartNumReq;
import com.hxzy.controller.mobile.cart.resp.MobileCartResp;
import com.hxzy.entity.ShoppingCart;
import com.hxzy.mapstruct.ShopCartMapStruct;
import com.hxzy.service.ShoppingCartService;
import com.hxzy.mapper.ShoppingCartMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author admin
* @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
* @createDate 2024-02-21 11:30:41
*/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
    implements ShoppingCartService{

    /**
     * 用户某个商家的购物车信息
     * @param merchantId
     * @return
     */
    @Override
    public List<MobileCartResp> cartList(Long merchantId) {
        long currentId = StpMobileUtil.getLoginIdAsLong();
        return this.baseMapper.cartList(merchantId,currentId);
    }

    @Override
    public int cartAdd(CartAddReq req) {
        ShoppingCart db = getShoppingCart(req.getMerchantId(),req.getDishId());
        int number=1;
        //没有数据,新增
        if(Objects.isNull(db)){
            ShoppingCart entity= ShopCartMapStruct.INSTANCE.convert(req);
            entity.setNumber(number);
            entity.setUserId(StpMobileUtil.getLoginIdAsLong());
            entity.setCreateTime(new Date());
            this.save(entity);
        }else{
            //做更新购物车数据，商品信息
            number=db.getNumber()+1;
            db.setNumber(number);
            db.setAmount(req.getAmount());
            db.setImage(req.getImage());
            db.setName(req.getName());
            db.setDishFlavor(req.getDishFlavor());
            this.updateById(db);
        }
        return number;
    }

    /**
     * 查询购物车当前用户，某个商家，某个商品信息
     * @param merchantId
     * @param dishId
     * @return
     */
    private ShoppingCart getShoppingCart(Long merchantId, Long dishId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getMerchantId, merchantId);
        queryWrapper.eq(ShoppingCart::getDishId, dishId);
        queryWrapper.eq(ShoppingCart::getUserId,StpMobileUtil.getLoginIdAsLong());
        ShoppingCart db = this.getOne(queryWrapper);
        return db;
    }

    /**
     * 添加数量
     * @param req
     * @return
     */
    @Override
    public int incrementNum(MobileAddCartNumReq req) {
        ShoppingCart db = getShoppingCart(req.getMerchantId(),req.getDishId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }

        db.setNumber(db.getNumber()+1);
        this.updateById(db);
        return db.getNumber();
    }

    /**
     * 减少数量
     * @param req
     * @return
     */
    @Override
    public int subNum(MobileAddCartNumReq req) {
        ShoppingCart db = getShoppingCart(req.getMerchantId(),req.getDishId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }
        //删除这个数据
        if(db.getNumber()==1){
            this.removeById(db.getId());
            return 0;
        }

        db.setNumber(db.getNumber()-1);
        this.updateById(db);
        return db.getNumber();
    }

    @Override
    public boolean clearMerchantIdAndProductId(MobileAddCartNumReq req) {
        ShoppingCart db = getShoppingCart(req.getMerchantId(),req.getDishId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }
        return this.removeById(db.getId());
    }

    /**
     * 清空当前用户某个商家的购物车所有数据
     * @param merchantId
     * @return
     */
    @Override
    public boolean cleanCart(Long merchantId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getMerchantId, merchantId);
        queryWrapper.eq(ShoppingCart::getUserId,StpMobileUtil.getLoginIdAsLong());

        return this.remove(queryWrapper);
    }
}




